{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "from PIL import Image, ImageDraw\n",
    "\n",
    "from src.input_pipeline import Pipeline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get images and boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "pipeline = Pipeline(\n",
    "    ['data/train_shards/shard-0000.tfrecords'],\n",
    "    batch_size=24, image_size=(1024, 1024),\n",
    "    repeat=False, shuffle=False, \n",
    "    augmentation=True\n",
    ")\n",
    "features, labels = pipeline.get_batch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with tf.Session() as sess:\n",
    "    I, B, N = sess.run([\n",
    "        features['images'], labels['boxes'], labels['num_boxes']\n",
    "    ])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Show an augmented image with boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_boxes(image, boxes):\n",
    "    image_copy = image.copy()\n",
    "    draw = ImageDraw.Draw(image_copy, 'RGBA')\n",
    "    width, height = image.size\n",
    "\n",
    "    for box in boxes:\n",
    "        ymin, xmin, ymax, xmax = box\n",
    "        xmin, xmax = width*xmin, width*xmax\n",
    "        ymin, ymax = height*ymin, height*ymax\n",
    "\n",
    "        fill = (255, 255, 255, 45)\n",
    "        outline = 'black'\n",
    "\n",
    "        draw.rectangle(\n",
    "            [(xmin, ymin), (xmax, ymax)],\n",
    "            fill=fill, outline=outline\n",
    "        )\n",
    "\n",
    "    return image_copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# choose an image\n",
    "i = 0\n",
    "image = Image.fromarray((np.transpose(I[i], [1, 2, 0])*255.0).astype('uint8'))\n",
    "num_boxes = N[i]\n",
    "boxes = B[i][:num_boxes]\n",
    "\n",
    "draw_boxes(image, boxes)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
